﻿// beta

// 建立 Sprite
// drawLine = 線 (can anime)
// drawCircle = 圓 (can anime)
// drawWave = 波浪線 (can anime)
// Rect = 方
// RoundRect = 圓角方

// 動畫支援效果需於
// this.reset() 或於建立後設定
// this.speed=Number; // 以秒為單位
// this.clockwise=Boolean; // 順/逆時秒繪畫
// this.lineMode = Number;
// this.bgColor = uint

package com.cf.core.view{
	// TweenMAX
	import com.greensock.*;
	import com.greensock.easing.*;
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	import flash.events.Event;
	import flash.utils.setTimeout;
	import flash.utils.setInterval;
	import flash.utils.clearTimeout;
	import flash.utils.clearInterval;
	import com.cf.core.Utils.tools;
	import com.cf.core.control.SyncSprite;
	public class MySprite extends SyncSprite{
		private var __speed:Number;
		public function get speed():Number{return __speed}
		public function set speed(_n:Number){__speed=_n}
		private var __lineStyle:Number;
		public function get lineMode():Number{return __lineStyle}
		public function set lineMode(_n:Number){__lineStyle=_n}
		private var __bgColor:uint;
		public function get bgColor():uint{return __bgColor}
		public function set bgColor(_n:uint){__bgColor=_n}
		private var __lineColor:uint;
		public function get lineColor():uint{return __lineColor}
		public function set lineColor(_n:uint){__lineColor=_n}
		private var __pos:Array;
		private var __clockwise:Boolean; // 順時針運動(true)
		public function get clockwise():Boolean{return __clockwise}
		public function set clockwise(_t:Boolean){__clockwise=_t}
		private var __playStop:Boolean; // 中途暫停 (loop checking)
		private var __myTween:TweenMax;
		
		private var __fixSpriteSize:Sprite;
		public function MySprite():void{
			reset();
		}//fn
		
		public function drawLine(_bx:Number, _by:Number, _ex:Number, _ey:Number):void{
			graphics.lineStyle (lineMode, lineColor);
			graphics.moveTo (_bx, _by);
			x = _bx;
			y = _by;
			handleFixSize( (_ex-_bx) , (_ey-_by) , _bx, _by);
			if(speed==0){
				graphics.lineTo (_ex, _ey);
			}else{
				// draw by animation
				__pos = new Array();
				__pos[0] = _bx;
				__pos[1] = _by;
				__pos[2] = _ex;
				__pos[3] = _ey;
				addEventListener(Event.ENTER_FRAME, handleDrawLine);
				__myTween = TweenMax.to(__pos, speed, {endArray:[_ex, _ey]});
			}//if
		}//fn
		private function handleDrawLine(e:Event):void{
			if(__playStop){
				//trace( __pos[0] +", "+ __pos[1] );
				graphics.lineTo(__pos[0], __pos[1]); // changing X,Y value.
				if(__pos[0]==__pos[2] && __pos[1]==__pos[3]){
					removeEventListener(Event.ENTER_FRAME, handleDrawLine);
				}//if
			}//if
		}//fn
		public function drawCircle(_centerX:Number, _centerY:Number, _radius:Number):void{
			if(lineColor<=0xFFFFFF) graphics.lineStyle (lineMode, lineColor);
			// x = _centerX;
			// y = _centerY;
			handleFixSize(_radius*2, _radius*2, 0, 0);
			//_centerX =0;
			//_centerY =0;
			graphics.moveTo (_centerX+_radius, _centerY);
			if(speed==0){
				graphics.beginFill(bgColor);
				graphics.drawCircle(_centerX,_centerY,_radius);
				graphics.endFill();
			}else{
				graphics.beginFill(bgColor);
				graphics.moveTo (_centerX, _centerY);
				__pos[0] = 0; // angle;
				__pos[1] = 0; // x;
				__pos[2] = 0; // y;
				__pos[3] = _radius; // radius
				__pos[4] = _centerX; // center X
				__pos[5] = _centerY; // center Y
				__pos[6] = 0.05; // Most small pixal in Flash, should not change.
				__pos[7] = 0; // checksum
				addEventListener (Event.ENTER_FRAME, handleDrawCircle);
				__myTween = TweenMax.to(__pos, speed, {endArray:[360], ease:Linear.easeNone}); // tween the first array only
			}//if
		}//fn
		private function handleDrawCircle(e:Event=null):void{
			if(__playStop){
				if(__pos[0]>=360){
					// close circle
					removeEventListener (Event.ENTER_FRAME, handleDrawCircle);
					graphics.endFill();
					// We got bugs, the last angle didn't Fill.
					graphics.clear();
					if(lineColor<=0xFFFFFF) graphics.lineStyle (lineMode, lineColor);
					graphics.beginFill(bgColor);
					graphics.drawCircle(__pos[4],__pos[5],__pos[3]);
					graphics.endFill();
					// trace(this+" drawCircle finish");
				}//if
				while(Math.abs(__pos[7]) < __pos[0]){
					// __pos[0] is the angle system request
					// __pos[7] is the angle of the graph currect drawing.
					__pos[1] = __pos[4] + Math.cos( (__pos[7]/180*Math.PI) ) * __pos[3]; // xpos = centerX + Math.cos(angle) * radius;
					__pos[2] = __pos[5] + Math.sin( (__pos[7]/180*Math.PI) ) * __pos[3]; // ypos = centerY + Math.sin(angle) * radius;
					// draw on the board
					graphics.lineTo (__pos[1], __pos[2] );
					if(clockwise){
						__pos[7] += __pos[6];
					}else{
						__pos[7] -= __pos[6];
					}//if
				}//do
				// trace(__pos[0] +" - "+ __pos[7]);
			}//if
		}//fn
		public function Rect(_l:Number, _t:Number, _w:Number, _h:Number):void{
			graphics.clear();
			if(lineColor<=0xFFFFFF) graphics.lineStyle (lineMode, lineColor);
			graphics.beginFill(bgColor);
			graphics.drawRect(_l, _t, _w, _h);
			graphics.endFill();
		}//fn
		public function RoundRect(_x:Number,_y:Number,_w:Number,_h:Number,_ew:Number,_eh:Number):void{
			//var rectangle:Shape = new Shape; // initializing the variable named rectangle
			graphics.clear();
			if(lineColor<=0xFFFFFF) graphics.lineStyle (lineMode, lineColor);
			graphics.beginFill(bgColor);
			graphics.drawRoundRect(_x, _y, _w, _h, _ew, _eh);			//(0, 0, 400, 100, 50, 50);
			graphics.endFill();
		}//fn
		
		/*
		public function drawWave():void{
			var speedX:Number = 1;
			var speedAngle:Number = 0.1;
			var range:Number = 100;
			var angle:Number = 0;
			var xpos:Number = 0;
			var ypos:Number = 0;
			var centerY:Number = stage.stageHeight / 2;


			graphics.lineStyle (2, 0xffffff);
			graphics.moveTo (0, centerY);
			addEventListener (Event.ENTER_FRAME, onEnterFrame);

			function onEnterFrame (event:Event):void {
			xpos += speedX;
			ypos = centerY + Math.sin(angle) * range;
			angle += speedAngle;
			graphics.lineTo (xpos,ypos);
		}//fn */
		
		public function reset():void{
			graphics.clear();
			__pos=new Array();
			__lineStyle=0;
			__speed=0;
			__bgColor=16777216;		// color will not over this range
			__lineColor=16777216;	// color will not over this range
			__clockwise=true;
			__playStop=true;
		}//fn
		public function stop():void{
			__playStop=false;
			__myTween.pause();
		}//fn
		public function play():void{
			__playStop=true;
			__myTween.resume();
		}//fn
		private function handleFixSize(_w:Number,_h:Number, _fx:Number=0, _fy:Number=0):void{
			// for circle & line those shape won't relay on Width & Height
			// add child as the same size let it return the target width, height we wanted
			tools.emptySprite(this);
			__fixSpriteSize = new Sprite();
			__fixSpriteSize.graphics.clear();
			__fixSpriteSize.graphics.beginFill(0xFF0000);
			__fixSpriteSize.graphics.drawRect(0, 0, _w, _h);
			__fixSpriteSize.graphics.endFill();
			__fixSpriteSize.alpha=0;
			__fixSpriteSize.x+=_fx;
			__fixSpriteSize.y+=_fy;
			addChild(__fixSpriteSize);
			// trace("MySprite fix size: w="+ __fixSpriteSize.width+" h="+__fixSpriteSize.height);
		}//fn
	}//class
}//package